package cn.tedu.controller;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*登录权限拦截器:从Session中检查用户的登录状态,如果登录了就放行,如果没有就登录拦截,发起重定向跳到登录页面*/
        String loginuser = (String) request
                .getSession().getAttribute("loginuser");
        System.out.println("当前用户:" + loginuser);
        if (loginuser == null) {
            System.out.println("没有登录，拦截");
            /*session中没有登录用户信息, 当前用户还没有登录
            发起重定向, 跳转到登录页面*/
            String path = request.getContextPath() + "/user/login.do";
            response.sendRedirect(path);
            /*返回false进行拦截*/
            return false;
        }
        System.out.println("已经登录，放行");
        return true;
    }

}
